# -*- coding: utf-8 -*-


from PyQt4.QtGui import *
from PyQt4.QtCore import *

import os
import sys
import gettext
import hashlib
import time

#To import kontrolcu's library \\ No need to write try: except: here
MODULEPATH = os.getcwd()[:-3] + "src"
sys.path.append(MODULEPATH)
from libkontrolcu import *

gettext.install("kontrolcu", unicode=1)

LOGOPATH = ":/icons/data/kontrolcu.png"

bytes = 1024**2

class TextDialog(QDialog):
    def __init__(self, parent=None, message={}):
        super(QDialog, self).__init__(parent)
        mainvertical = QVBoxLayout()
        buttonlayout = QHBoxLayout()
        self.browser = QTextBrowser()
        self.cliptext = ""
        if message is not None:
            for i in message.keys():
                self.cliptext += i + " "
                if message[i] == "+":
                    self.browser.append("<font color=green>" + i + "</font>")
                elif message[i] == "-":
                    self.browser.append("<font color=red>" + i + "</font>")
                elif message[i] == "0":
                    self.browser.append("<font color=blue>" + i + "</font>")
                else:
                    self.browser.append(i)
        else:
            self.broself.cliptextwser.append("<font color=red>" +_("There was no message!") +"</font>")

        self.quitme = QPushButton(_("Close"))
        self.copyToClipboard = QPushButton(_("Copy to Clipboard"))
        self.saveToFile = QPushButton(_("Save To File"))


        mainvertical.addWidget(self.browser)
        buttonlayout.addWidget(self.copyToClipboard)
        buttonlayout.addWidget(self.saveToFile)
        buttonlayout.addWidget(self.quitme)
        mainvertical.addLayout(buttonlayout)

        self.setLayout(mainvertical)
        self.setWindowTitle("Kontrolcu")
        
        self.icon = QIcon(LOGOPATH)
        self.setWindowIcon(self.icon)

        self.connect(self.quitme, SIGNAL("clicked()"), self.close)
        self.connect(self.copyToClipboard, SIGNAL("clicked()"), self.copyClip)
        self.connect(self.saveToFile, SIGNAL("clicked()"), self.saveFile)
    def copyClip(self):
        clipboard = QApplication.clipboard()
        if self.cliptext:
            clipboard.setText(self.cliptext)

    def saveFile(self):
        text = self.cliptext
        newfilepath = QFileDialog.getSaveFileName(self, "Kontrolcu" +  _("- Save the Check"), _("unnamed"))
        try:
            openfile = open(newfilepath, 'w')
            openfile.write(text)
            openfile.close()
        except IOError, (error_no, error):
            if error_no is 13:
                errortext= _("Error:(while saving) You don't have permission to save.")
                QMessageBox.critical(self, "Kontrolcu" + " - " + _("Error"), errortext)
            else:
                errortext= _("Error:(unknown) or user closed the save dialog.")
                QMessageBox.critical(self, "Kontrolcu" + " - " + _("Error"), errortext)


class CheckDialog(QDialog):
    def __init__(self, parent=None, worklist=[], finalnumber=0):
        super(CheckDialog, self).__init__(parent)
        mainvertical = QVBoxLayout()
        lasthorizontal = QHBoxLayout()
        self.informlabel = QLabel(_("Checking:"))
        self.progress = QProgressBar()
        self.size = finalnumber
        self.progress.setMinimum(0)
        self.progress.setMaximum(self.size/bytes)
        self.cancelbutton = QPushButton(_("Cancel"))
        self.totalsizelabel = QLabel(_("Final Size:") +" "+str(self.size/(1024)**2)+ " " + _("megabytes"))
        lasthorizontal.addWidget(self.totalsizelabel)
        lasthorizontal.addStretch(3)
        lasthorizontal.addWidget(self.cancelbutton)
        self.filename= QLabel()
        mainvertical.addWidget(self.informlabel)
        mainvertical.addWidget(self.filename)
        mainvertical.addWidget(self.progress)
        mainvertical.addLayout(lasthorizontal)
        self.setLayout(mainvertical)
        self.setWindowTitle(_("Kontrolcu - Check"))
        self.icon = QIcon(LOGOPATH)
        self.setWindowIcon(self.icon)




class ProgressIncrement(QThread): #Thanks Gokmen Gorgen's progressbar! :)
    def __init__(self, progressbar, checktype=None, filelist=[], label=None): #listwidget=None
        QThread.__init__(self)
        self.pbar= progressbar
        self.ctype = checktype
        self.clist = filelist
        self.resultlist = []
        self.mylabel = label
	#self.listwid = listwidget
    def run(self):
        #self.listwid.clear()
        for i in self.clist:
            chash = self.ctype()
            draft = ""
            bytes = 1024**2
            self.rfile = i.split(os.sep)[-1]
            if len(self.rfile) > 42:
                 self.mylabel.setText(self.rfile[:30] + "...")
            else:
                 self.mylabel.setText(self.rfile)
            draft = "  " + self.rfile
            ifile = file(i, "rb")
            while bytes:
                data = ifile.read(bytes)
                chash.update(data)
                bytes = len(data)
                self.emit(SIGNAL("incrementProgressBar()"))
            draft = str(chash.hexdigest()) + draft
            print makeGreen("<- " + draft)
            self.resultlist.append(draft)
        print makeYellow(_("Operation is finished!"))
        self.emit(SIGNAL("operationFinished()"))
       
    def incrementProgressBar(self):
	try:
            current_value = self.pbar.value()
            self.pbar.setValue(current_value + 1 )
        except:
            pass

        
import icons_rc